package org.nimi317.web_gis.utils;


import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author thunderobot
 */
public class ListUtils {


    public static String listToString(List<?> list) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> iterator = list.iterator();
        boolean next = iterator.hasNext();
        while (next) {
            sb.append(iterator.next());
            next = iterator.hasNext();
            if (next) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    /**
     * 两个参数 第一个是对传入对象所有进行处理 第二个是非最后一项时进行处理
     *
     * @return
     */
    public static <T> void forHandle(Collection<T> collection, Consumer<T> all, Consumer<T> notLast) {
        Iterator<T> iterator = collection.iterator();
        boolean next = iterator.hasNext();
        while (next) {
            T nexted = iterator.next();
            all.accept(nexted);
            next = iterator.hasNext();
            if (next) {
                notLast.accept(nexted);
            }
        }
    }

    public static <T> void forNextHandle(Collection<T> collection, Consumer<T> all, Consumer<T> notLast) {
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
            T nexted = iterator.next();
            all.accept(nexted);
            if (iterator.hasNext()) {
                nexted = iterator.next();
                notLast.accept(nexted);
            }
        }
    }

    public static String stringAddBracket(String s) {
        if (s.isBlank()) {
            return "";
        }
        return "(" + s + ")";
    }
}
